package com.atguigu.spzx.manager.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.model.entity.system.Category;
import com.atguigu.spzx.model.entity.system.CategoryExcelVo;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;


@NoArgsConstructor
public class ExcelListener extends AnalysisEventListener<CategoryExcelVo> {
    CategoryMapper categoryMapper;
    List<CategoryExcelVo> car = new ArrayList<>();
    public ExcelListener(CategoryMapper categoryMapper){
        this.categoryMapper=categoryMapper;
    }

    @Override
    public void invoke(CategoryExcelVo categoryExcelVo, AnalysisContext analysisContext) {
        car.add(categoryExcelVo);
        if(car.size()>=100){
            List<Category> categories = new ArrayList<>();
            for (CategoryExcelVo excelVo : car) {
                Category category = new Category();
                BeanUtils.copyProperties(excelVo,category);
                categories.add(category);
            }
            categoryMapper.batchInsert(categories);
            car.clear();
        }
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("最后一次："+car.size());
    }
}
